#include "../include/string.h"
#include "../include/game.h"


int contarFichas(int tablero[9], int tty){
	int i;
	int fichas = 0;
	for(i = 0; i < 9; ++i){
		if(tablero[i] == tty){
			++fichas;
		}
	}
	return fichas;
}
/* retorna 0 si gano, de lo contrario -1 */
int gane(int tablero[9], int tty){

	if(contarFichas(tablero,tty) != 3){
		return -1;
	}

	if(tablero[0] == tty ){
		if(tablero[1] == tty){
			if(tablero[2] == tty){
				return 0;
			}else{
				return -1;
			}
		}else if(tablero[4] == tty){
			if(tablero[8] == tty){
				return 0;
			}else{
				return -1;
			}
		}else if(tablero[3] == tty){
			if(tablero[6] == tty){
				return 0;
			}else{
				return -1;
			}
		}else{
			return -1;
		}
	}else if(tablero[1] == tty){
		if(tablero[4] == tty){
			if(tablero[7] == tty){
				return 0;
			}else{
				return -1;
			}
		}else{
			return -1;
		}
	}else if(tablero[2] == tty){
		if(tablero[4] == tty){
			if(tablero[6] == tty){
				return 0;
			}else{
				return -1;
			}
		}else if(tablero[5] == tty){
			if(tablero[8] == tty){
				return 0;
			}else{
				return -1;
			}
		}else{
			return -1;
		}
	}else if(tablero[3] == tty){
		if(tablero[4] == tty){
			if(tablero[5] == tty){
				return 0;
			}else
				return -1;
		}else{
			return -1;
		}
	}else if(tablero[6] == tty){
		if(tablero[7] == tty){
			if(tablero[8] == tty){
				return 0;
			}else{
				return -1;
			}
		}else{
			return -1;
		}
	}else{
		return -1;
	}
}

int readBoard(int r_fifo, int tablero[9]){
	while(readFifo(r_fifo,tablero,9 * 4) == -1){
		;
	}
	return ;
}

int writeBoard(int w_fifo, int tablero[9]){
	while(writeFifo(w_fifo, tablero, 9*4) == -1){
		;
	}
	return ;
}

void jugar(int yo, int otro, int w_fifo, int r_fifo){

	int fichas;
	int tablero[9];
	int position;
	int from_to[2];

	readBoard(r_fifo,tablero);
	while(gane(tablero,otro) == -1){
		fichas = contarFichas(tablero,yo);
		if(fichas < 3){
			limpiaPantalla();
			print("Su contrincante jugo\n");
			printTablero(tablero);
			position = getOperation1();
			while(ponerFicha(yo,position,tablero)== 0){
				position = getOperation1();
			}
			limpiaPantalla();
			print("Usted jugo\n");
			printTablero(tablero);
			writeBoard(w_fifo,tablero);
			if(gane(tablero,yo) != -1){
				limpiaPantalla();
				print("Felicitaciones ha ganado\n");
				return;
			}
			//writeBoard(w_fifo,tablero);
		}else{
			limpiaPantalla();
			print("Su contrincante jugo\n");
			printTablero(tablero);
			getOperation2(from_to);
			while(moverFicha(yo,from_to[0],from_to[1],tablero) == 0){
				getOperation2(from_to);
			}
			limpiaPantalla();
			print("Usted jugo\n");
			printTablero(tablero);
			writeBoard(w_fifo,tablero);
			if(gane(tablero,yo) != -1){
				print("Felicitaciones ha ganado\n");
				return;
			}
			//writeBoard(w_fifo,tablero);
		}
		print("Espere su turno\n");
		readBoard(r_fifo, tablero);

	}
	limpiaPantalla();
	print("Ha perdido\n");
}

void server(int yo, int otro, int w_fifo, int r_fifo){

	int i;
	int tablero[9];
	int position;

	for(i = 0; i < 9; ++i){
			tablero[i] = -1;
	}
	limpiaPantalla();
	print("Cuando se le solicite donde desea jugar ingrese el numero correspondiente a la\n");
	print(" posicion deseada segun el siguiente esquema\n");
	print("0-1-2\n");
	print("| | |\n");
	print("3-4-5\n");
	print("| | |\n");
	print("6-7-8\n");
	print("\n");
	printTablero(tablero);
	position = getOperation1();
	while(ponerFicha(yo,position,tablero) == 0){
		position = getOperation1();
	}
	print("Usted jugo\n");
	printTablero(tablero);
	writeBoard(w_fifo,tablero);
	jugar(yo, otro, w_fifo, r_fifo);
}

void client(int yo, int otro, int w_fifo, int r_fifo){
	limpiaPantalla();
	print("Comienza su contrincante\n");
	print("Cuando se le solicite donde desea jugar ingrese el numero correspondiente a la\n");
	print(" posicion deseada segun el siguiente esquema\n");
	print("0-1-2\n");
	print("| | |\n");
	print("3-4-5\n");
	print("| | |\n");
	print("6-7-8\n");
	print("\n");
	jugar(yo, otro, w_fifo, r_fifo);
}


void printTablero(int tablero[]) {
	int i;
	for (i = 0; i < 9; i++) {
		if (tablero[i] == -1) {
			print("O");
		} else {
			printd(tablero[i] + 1);
		}
		if (i % 3 != 2) {
			print("****");
		} else {
			if (i == 9) {
				print("\n");
			} else if (i == 2) {
				print("\n**   *   **\n* *  *  * *\n*  * * *  *\n*   ***   *\n");
			} else if (i == 5) {
				print("\n*   ***   *\n*  * * *  *\n* *  *  * *\n**   *   **\n");
			}
		}
	}
	print("\n");
}
int morethan3moves(int tablero[], int player) {
	int i;
	int count = 0;
	for (i = 0; i < 9; i++) {
		if (tablero[i] == player) {
			count++;
		}
	}
	return count == 3 ? 1 : count >= 3 ? -1 : 0;
}

int validmove(int from, int to) {
	int moves[][9] = {
	/*	  0  1  2  3  4  5  6  7  8 */
	{ 0, 1, 0, 1, 1, 0, 0, 0, 0 }, { 1, 0, 1, 0, 1, 0, 0, 0, 0 }, { 0, 1, 0, 0,
			1, 1, 0, 0, 0 }, { 1, 0, 0, 0, 1, 0, 1, 0, 0 }, { 1, 1, 1, 1, 0, 1,
			1, 1, 1 }, { 0, 0, 1, 0, 1, 0, 0, 0, 1 }, { 0, 0, 0, 1, 1, 0, 0, 1,
			0 }, { 0, 0, 0, 0, 1, 0, 1, 0, 1 }, { 0, 0, 0, 0, 1, 1, 1, 0, 0 } };
	if (from < 0 || from > 9) {
		return 0;
	} else {
		return moves[from][to];
	}
}
/*
 * Retorna 1 si puso la ficha,
 * 0 si no se pudo poner
 */
int ponerFicha(int player, int move, int board[]) {
	if (board[move] != -1) {
		return 0;
	} else {
		if(!morethan3moves(board,player)){
			board[move] = player;
			return 1;
		}
	}
	return 0;
}
/*
 * 1 si la movio
 * 0 si no
 */
int moverFicha(int player, int from, int to, int board[]) {
	if(from == -1){
		return 0;
	}
	if (board[to] != -1) {
		return 0;
	} else {
		if(!validmove(from,to) || board[from] != player){
			return 0;
		}
	}
	board[from] = -1;
	board[to] = player;
	return 1;
}

int getOperation1(){
	unsigned char c;
	int move;
	char s[2];
	print("Donde?: ");
	c = getChar();
	putchar(c);
	s[0] = c;
	s[1] = '\0';
	print("\n");
	while( atoi(s) < 0 || atoi(s) > 8){
		print(".........\n");
		print("Donde?: ");
		c = getChar();
		putchar(c);
		s[0] = c;
		print("\n");
	}
	move = c - '0';
	return move;
}


void getOperation2(int operations[2]){
	unsigned char c,d;
	int from, to;
	char s1[2],s2[2];
	print("Desde:");
	c = getChar();
	putchar(c);
	s1[0] = c;
	s1[1] = '\0';
	print("\nHasta:");
	d = getChar();
	s2[0] = d;
	s2[1] = '\0';
	putchar(d);
	print("\n");
	while( atoi(s1) < 0 || atoi(s1) > 8 || atoi(s2) < 0 || atoi(s2) > 8){
		print(".........\n");
		print("Desde:");
		c = getChar();
		putchar(c);
		s1[0] = c;
		print("\nHasta:");
		d = getChar();
		putchar(d);
		s2[0] = d;
		print("\n");
	}
	from = c - '0';
	to = d - '0';
	operations[0] = from;
	operations[1] = to;
}


/*
 0****1****0
 **   *   **
 * *  *  * *
 *  * * *  *
 *   ***   *
 1****1****2
 *   ***   *
 *  * * *  *
 * *  *  * *
 **   *   **
 1****2****1
 */

